{% extends 'index.html' %}

{% block sidebar %}{% endblock %}

{% block content %}
    <form class="form-horizontal" role="form" method="post">
        {% csrf_token %}
        {% for field in form_obj %}
            <div class="form-group">
                <label for="" class="col-sm-1 control-label">
                    {{ field.label }}
                </label>
                <div class="col-md-5 col-sm-5">
                    {{ field }}
                    {% for error_data in field.errors.get_json_data %}
                        <span style="color: red; margin-right: 5px">{{ error_data.message }};</span>
                    {% endfor %}
                </div>
            </div>
        {% endfor %}

        <div class="row">
            <div class="col-md-3 col-sm-3 col-sm-offset-3 " style="color: red">{{ form_obj.non_field_errors.0 }}</div>
        </div>

        <a href="/backend/Project/preview/" class="btn btn-info col-sm-offset-1">取消并返回</a>
        <button type="submit" class="btn btn-success col-sm-offset-3" >保存</button>
    </form>
    <div class="modal fade" id="test_git_failure" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width:400px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Error</h4>
            </div>
            <div class="modal-body" id="tes_git_error_msg"></div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
    {% include 'include/common/built_in_error_msg_modal.html' %}
{% endblock %}

{% block custome_js %}
    <script>
        $(function () {
            $('select[name="auth"]').change(function () {
                if($('input[name="git_address"]').val()&&$(this).val()){
                     test_git();    // 验证Git地址
                 }
            });
            $('input[name="git_address"]').change(function () {
                 if($('select[name="auth"]').val()){
                     test_git();    // 验证Git地址
                 }
            });
            function test_git(){
                $.ajax({
                    url : "{% url 'test_git' %}",
                    type: 'POST',
                    data: {'git_address': $('input[name="git_address"]').val(), 'auth': $('select[name="auth"]').val()},
                    dataType: 'JSON',
                    success: function (res) {
                        if(!res['status']){
                            $('#tes_git_error_msg').text(res['error']);
                                $('#test_git_failure').modal();
                        }
                    },
                    error: function (res) {
                        $('#built_in_error_msg_modal').modal();
                    }
                });
            }

        });
    </script>

{% endblock %}